/**
 * Copyright 2011 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.codelab;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
//import com.google.appengine.api.datastore.Key;
//import com.google.appengine.api.datastore.KeyFactory;

/**
 * This class defines the methods for basic operations of create, update & retrieve
 * for Cliente entity
 * 
 * @author 
 *
 */
public class Cliente {

	/**
	 * Checks if the entity is existing and if it is not, it creates the entity
	 * else it updates the entity
	 * 
	 * @param cliente_name
	 *          : username for the Cliente
	 * @param cliente_firstName
	 *          : first name of the Cliente
	 * @param cliente_lastName
	 *          : last name of the Cliente
	 * @param cliente_phone
	 *          : phone of Cliente
	 * @param cliente_cpf
	 *          : cpf phone number
	 * @param cliente_email
	 *          : email of Cliente         
	 * @param localidade_id
	 *          : localidade id of Cliente
	 */
  public static void createOrUpdateCliente(String cliente_id, String cliente_name, String cliente_firstName,
			String cliente_lastName, String cliente_phone, String cliente_cpf, String cliente_email, String localidade_id) {
    Entity cliente = getSingleCliente(cliente_id);
    Entity localidade = Localidade.getSingleLocalidade(localidade_id);
    if (cliente == null) {
      cliente = new Entity("Cliente", cliente_id);
      cliente.setProperty("cliente_id", cliente_id);
      cliente.setProperty("cliente_name", cliente_name);
      cliente.setProperty("cliente_firstName", cliente_firstName);
      cliente.setProperty("cliente_lastName", cliente_lastName);
      cliente.setProperty("cliente_phone", cliente_phone);
      cliente.setProperty("cliente_cpf", cliente_cpf);
      cliente.setProperty("cliente_email", cliente_email);
      cliente.setProperty("localidade_id", localidade.getProperty("localidade_id").toString());
    } else {
      if (cliente_name != null && !"".equals(cliente_name)) {
        cliente.setProperty("cliente_name", cliente_name);
      }
      if (cliente_firstName != null && !"".equals(cliente_firstName)) {
          cliente.setProperty("cliente_firstName", cliente_firstName);
        }
      if (cliente_lastName != null && !"".equals(cliente_lastName)) {
        cliente.setProperty("cliente_lastName", cliente_lastName);
      }
      if (cliente_phone != null && !"".equals(cliente_phone)) {
        cliente.setProperty("cliente_phone", cliente_phone);
      }
      if (cliente_cpf != null && !"".equals(cliente_cpf)) {
        cliente.setProperty("cliente_cpf", cliente_cpf);
      }
      if (cliente_email != null && !"".equals(cliente_email)) {
        cliente.setProperty("cliente_email", cliente_email);
      }
      
    }
    Util.persistEntity(cliente);
  }

	/**
	 * List all the clientes available
	 * 
	 * @return an iterable list with all the clientes
	 */
  public static Iterable<Entity> getAllClientes() {
    Iterable<Entity> entities = Util.listEntities("Cliente", null, null);
    return entities;
  }

	/**
	 * Searches for a Cliente and returns the entity as an iterable The search is
	 * performed by creating a query and searching for the attribute
	 * 
	 * @param clienteName
	 *          : username of the cliente
	 * @return iterable with the clientes searched for
	 */
  public static Iterable<Entity> getCliente(String cliente_id) {
    Iterable<Entity> entities = Util.listEntities("Cliente","cliente_id", cliente_id);
    return entities;
  }

  
	/**
	 * Searches for a Cliente and returns the entity as an iterable The search
	 * is performed by creating a query and searching for the attribute
	 * 
	 * @param clienteName
	 *            : username of the cliente
	 * @return iterable with the clientes searched for
	 */
	public static Iterable<Entity> getClientebyName(String cliente_name) {
		Iterable<Entity> entities = Util.listEntities("Cliente", "cliente_name",
				cliente_name);
		return entities;
	}
	
	
	/**
	 * Searches for a cliente and returns the entity as an iterable The search is
	 * key based instead of query
	 * 
	 * @param clienteName
	 *          : username of the cliente
	 * @return the entity with the username as key
	 */
  public static Entity getSingleCliente(String cliente_id) {
	    Iterable<Entity> results = Util.listEntities("Cliente", "cliente_id", cliente_id);
	    List<Entity> entity = new ArrayList<Entity>();
	    for(Entity e : results)
	      if(e!=null)
	        entity.add(e);
	      if (!entity.isEmpty()) {
	        return (Entity)entity.remove(0);
	      }
		  return null;
  }
}
